

// 温度转颜色，简单的红转蓝，用于测试
export function Temperature2ColorValueRed2Blue(temperature: number,minValue = 0,maxValue = 50) {
    const coldColor = [0.0,0.0,1.0];
    const hotColor = [1.0,0.0,0.0];

    let colorValue;
    
    // 手动指定色带 0-50 blue-red
    if (temperature < minValue) {
        colorValue = coldColor;
    } else if (temperature > maxValue) {
        colorValue = hotColor;
    }
    else {
        const ratio = temperature / (maxValue - minValue);
        colorValue = [
            coldColor[0] * (1 - ratio) + hotColor[0] * ratio,
            coldColor[1] * (1 - ratio) + hotColor[1] * ratio,
            coldColor[2] * (1 - ratio) + hotColor[2] * ratio
        ]
    }
    return colorValue;
}




// TODO:做成色带接口，调整颜色效果

// 此函数用于根据输入值、最小值、最大值和分段个数生成对应的 RGB 颜色
export function Value2Color(value: number, lowValue: number = 10, highValue: number = 25, segmentCount: number = 10): number[] {
    // 检查输入值是否在设定的范围之间
    if (value < lowValue || value > highValue) {
        console.error(`Value must be between ${lowValue} and ${highValue}`);
        value = Math.min(Math.max(value, lowValue), highValue);
    }

    // 计算每个分段的步长
    const step = (highValue - lowValue) / segmentCount;
    // 计算输入值所在的分段索引
    const segmentIndex = Math.floor((value - lowValue) / step);
    // 计算当前分段的起始值
    const segmentStartValue = lowValue + segmentIndex * step;
    // 计算当前分段内的归一化值
    const segmentNormalizedValue = (value - segmentStartValue) / step;
    // 计算每个分段对应的色调值
    const hueStep = 240 / segmentCount;
    // 计算当前分段的起始色调值
    const startHue = segmentIndex * hueStep;
    // 计算当前值对应的色调值
    const hue = startHue + segmentNormalizedValue * hueStep;
    

    // INFO:上下两种方式计算出来的颜色效果差别不大
    // // 按照从红色（0）到绿色（120）再到蓝色（240）的过渡逻辑计算色调
    // let hue: number;
    // const overallNormalizedValue = (value - lowValue) / (highValue - lowValue);
    // if (overallNormalizedValue <= 0.5) {
    //     // 从红色（0）到绿色（120）的过渡
    //     hue = overallNormalizedValue * 2 * 120;
    // } else {
    //     // 从绿色（120）到蓝色（240）的过渡
    //     hue = 120 + (overallNormalizedValue - 0.5) * 2 * 120;
    // }

    // console.log("hue: ",hue);


    // 固定饱和度和亮度
    const saturation = 100; // 100% 饱和度
    const lightness = 50;   // 50% 亮度（中等亮度）

    // 返回 RGB 颜色数组
    return hslToRgb(hue, saturation, lightness,1);
}

// 此函数用于将 HSL 颜色值转换为 RGB 颜色值，考虑分段逻辑。segementCount=1与原来一致
function hslToRgb(h: number, s: number, l: number, segmentCount: number = 1): number[] {
    s /= 100;
    l /= 100;
    const c = (1 - Math.abs(2 * l - 1)) * s;
    const x = c * (1 - Math.abs((h / 60) % 2 - 1));
    const m = l - c / 2;
    let r, g, b;

    // 计算每个分段的色调范围
    const hueStep = 360 / segmentCount;
    const segmentIndex = Math.floor(h / hueStep);
    const segmentStartHue = segmentIndex * hueStep;
    const segmentEndHue = (segmentIndex + 1) * hueStep;

    // 根据分段内的相对位置调整颜色计算逻辑
    const relativeHue = h - segmentStartHue;
    if (0 <= relativeHue && relativeHue < hueStep / 6) {
        r = c; g = relativeHue / (hueStep / 6) * x; b = 0;
    } else if (hueStep / 6 <= relativeHue && relativeHue < 2 * hueStep / 6) {
        r = (1 - (relativeHue - hueStep / 6) / (hueStep / 6)) * x; g = c; b = 0;
    } else if (2 * hueStep / 6 <= relativeHue && relativeHue < 3 * hueStep / 6) {
        r = 0; g = c; b = (relativeHue - 2 * hueStep / 6) / (hueStep / 6) * x;
    } else if (3 * hueStep / 6 <= relativeHue && relativeHue < 4 * hueStep / 6) {
        r = 0; g = (1 - (relativeHue - 3 * hueStep / 6) / (hueStep / 6)) * x; b = c;
    } else if (4 * hueStep / 6 <= relativeHue && relativeHue < 5 * hueStep / 6) {
        r = (relativeHue - 4 * hueStep / 6) / (hueStep / 6) * x; g = 0; b = c;
    } else {
        r = c; g = 0; b = (1 - (relativeHue - 5 * hueStep / 6) / (hueStep / 6)) * x;
    }

    r = Math.round((r + m) * 255);
    g = Math.round((g + m) * 255);
    b = Math.round((b + m) * 255);

    return [r, g, b];
}


// 定义 10 个过渡颜色，从红色到绿色再到蓝色
const COLOR_PALETTE: number[][] = [
    [255, 0, 0],       // 红色
    [255, 63, 0],
    [255, 127, 0],
    [255, 191, 0],
    [255, 255, 0],     // 黄色
    [191, 255, 0],
    [127, 255, 0],
    [63, 255, 0],
    [0, 255, 0],       // 绿色
    [0, 255, 255],     // 青色
    [0, 127, 255],
    [0, 0, 255]        // 蓝色
];

// TODO:颜色还需调整
export function Value2ColorTenSegements(value: number, lowValue: number = 10, highValue: number = 25): number[] {
    // 检查输入值是否在设定的范围之间
    if (value < lowValue || value > highValue) {
        console.error(`Value must be between ${lowValue} and ${highValue}`);
        value = Math.min(Math.max(value, lowValue), highValue);
    }

    // 计算每个分段的步长
    const step = (highValue - lowValue) / 10;
    // 计算输入值所在的分段索引
    const segmentIndex = Math.floor((value - lowValue) / step);

    // 返回对应分段的颜色
    return COLOR_PALETTE[segmentIndex];
}

/**
 * 值域0~3.4，颜色依次为深蓝、浅蓝、绿、深绿、黄、红、深红
 */
export function Value2ColorBlueGreenYellowRedWithFixWQRValue(
    value: number,
    lowValue: number = 0,
    highValue: number = 3.4
): number[] {
    // 定义分段色带
    const COLOR_BAND: number[][] = [
        [0, 0, 128],     // 深蓝
        [0, 128, 255],   // 浅蓝
        [0, 255, 0],     // 绿
        [0, 128, 0],     // 深绿
        [255, 255, 0],   // 黄
        [255, 0, 0],     // 红
        [128, 0, 0],     // 深红
    ];
    const segmentCount = COLOR_BAND.length - 1;

    // 边界处理
    if (value <= lowValue) return COLOR_BAND[0];
    if (value >= highValue) return COLOR_BAND[segmentCount];

    // 计算分段
    const step = (highValue - lowValue) / segmentCount;
    const segmentIndex = Math.floor((value - lowValue) / step);
    const segmentStart = lowValue + segmentIndex * step;
    const t = (value - segmentStart) / step;

    // 线性插值
    const colorStart = COLOR_BAND[segmentIndex];
    const colorEnd = COLOR_BAND[segmentIndex + 1];
    return [
        Math.round(colorStart[0] + (colorEnd[0] - colorStart[0]) * t),
        Math.round(colorStart[1] + (colorEnd[1] - colorStart[1]) * t),
        Math.round(colorStart[2] + (colorEnd[2] - colorStart[2]) * t),
    ];
}
